package com.freak.wanandroid.model.home;

import android.content.Intent;
import android.view.View;
import android.widget.RelativeLayout;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.freak.wanandroid.R;
import com.freak.wanandroid.base.fragment.BaseLifecycleFragment;
import com.freak.wanandroid.bean.BannerBean;
import com.freak.wanandroid.bean.SystemClassChildrenEntity;
import com.freak.wanandroid.bean.ToDoEntity;
import com.freak.wanandroid.model.home.activity.BannerDetailAct;
import com.freak.wanandroid.model.home.adapter.HomeRollAdapter;
import com.freak.wanandroid.model.home.adapter.SpecialSubscriptionAdapter;
import com.freak.wanandroid.model.home.adapter.ToDoAdapter;
import com.jude.rollviewpager.RollPagerView;
import com.jude.rollviewpager.hintview.ColorPointHintView;

import java.util.ArrayList;
import java.util.List;

/***
 * 首页
 */
public class HomepageFragment extends BaseLifecycleFragment<HomePageViewModel> {

    private RecyclerView mRecycleViewSpecialSubscription;

    private RecyclerView mRecycleViewToDo;

    private RelativeLayout mNetErrorView;

    private RollPagerView mRollPagerView;

    private HomeRollAdapter mHomeRollAdapter;

    private ToDoAdapter mToDoAdapter;

    private List<ToDoEntity> mToDoEntityList;

    private SpecialSubscriptionAdapter mSpecialSubscriptionAdapter;

    private List<SystemClassChildrenEntity> mChildrenList;


    //    /***
//     * 断网重连
//     * @param networkType
//     */
//    @Override
//    public void onNetConnected(NetworkType networkType) {
//        super.onNetConnected(networkType);
//        curPage = 1;
//        mViewModel.getBanner();
//        mViewModel.getArticle(String.valueOf(curPage));
//    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_homepage;
    }

    @Override
    protected void dataObserver() {
        mViewModel.mGetBanner.observe(this, this::getBannerSuccess);
        mViewModel.mGetBannerError.observe(this, this::getBannerError);
        mViewModel.mChildrenMutableLiveData.observe(this, children -> {
            mChildrenList.clear();
            mChildrenList.addAll(children);
            mSpecialSubscriptionAdapter.notifyDataSetChanged();
        });
    }

    @Override
    protected void initEventAndData(View view) {
        initView(view);
    }

    private void initView(View view) {
        initBannerView(view);
        initSpecialSubscription(view);
        initTodo(view);
    }

    /**
     * 代办清单
     *
     * @param view View
     */
    private void initTodo(View view) {
        mToDoEntityList = new ArrayList<>();
        mRecycleViewToDo = view.findViewById(R.id.recycleViewToDo);
        mRecycleViewToDo.setLayoutManager(new LinearLayoutManager(mCurrentActivity));
        mToDoAdapter = new ToDoAdapter(R.layout.item_todo, mToDoEntityList);
        mToDoAdapter.bindToRecyclerView(mRecycleViewToDo);
        mToDoAdapter.addHeaderView(getTodoHeadView());
        mToDoAdapter.addFooterView(getTodoFooterView());
        mRecycleViewToDo.setAdapter(mToDoAdapter);
    }

    /**
     * 专题订阅列表
     *
     * @param view View
     */
    private void initSpecialSubscription(View view) {
        mChildrenList = new ArrayList<>();
        mRecycleViewSpecialSubscription = view.findViewById(R.id.recycleViewSpecialSubscription);
        mSpecialSubscriptionAdapter = new SpecialSubscriptionAdapter(R.layout.item_special_subscription, mChildrenList);
        mRecycleViewSpecialSubscription.setLayoutManager(new GridLayoutManager(mCurrentActivity, 2));
        mSpecialSubscriptionAdapter.bindToRecyclerView(mRecycleViewSpecialSubscription);
        mSpecialSubscriptionAdapter.addHeaderView(getSpecialSubscriptionHeadView());
        mSpecialSubscriptionAdapter.addFooterView(getSpecialSubscriptionFooterView());
        mRecycleViewSpecialSubscription.setAdapter(mSpecialSubscriptionAdapter);
    }

    private View getSpecialSubscriptionHeadView() {
        View view = getLayoutInflater().inflate(R.layout.item_special_subscription_head, mRecycleViewSpecialSubscription, false);
        return view;
    }

    private View getSpecialSubscriptionFooterView() {
        View view = getLayoutInflater().inflate(R.layout.item_special_subscription_footer, mRecycleViewSpecialSubscription, false);
        view.setOnClickListener(v -> {
            Intent intent = new Intent();
            intent.setClass(mCurrentActivity, SpecialSubscriptionActivity.class);
            startActivity(intent);
        });
        return view;
    }

    private View getTodoHeadView() {
        View view = getLayoutInflater().inflate(R.layout.item_todo_head, mRecycleViewToDo, false);
        return view;
    }

    private View getTodoFooterView() {
        View view = getLayoutInflater().inflate(R.layout.item_todo_footer, mRecycleViewToDo, false);
        return view;
    }



    private void initBannerView(View view) {
        mRollPagerView = view.findViewById(R.id.roll_pagerView);
        //设置播放时间间隔
        mRollPagerView.setPlayDelay(3000);
        //设置透明度
        mRollPagerView.setAnimationDurtion(500);
        mRollPagerView.setHintView(new ColorPointHintView(getActivity(), 0xffff6d1d, 0xffeeeeee));
    }

    @Override
    protected void onResumeLoadData() {
        mViewModel.getBanner();
        showLoading();
    }

    @Override
    public void onResume() {
        super.onResume();
        mViewModel.getSpecialSubscriptionInfo();
    }

    /***
     * 获取首页轮播图成功回调
     * @param list List<BannerBean>
     */
    public void getBannerSuccess(List<BannerBean> list) {
        dismissLoading();
        if (mHomeRollAdapter == null) {
            mHomeRollAdapter = new HomeRollAdapter(getActivity(), list);
            mRollPagerView.setAdapter(mHomeRollAdapter);
        } else {
            mHomeRollAdapter.setData(list);
            mHomeRollAdapter.notifyDataSetChanged();
        }
        mRollPagerView.setOnItemClickListener(position -> {
            Intent intent = new Intent(getActivity(), BannerDetailAct.class);
            intent.putExtra("url", list.get(position).getUrl());
            intent.putExtra("title", list.get(position).getTitle());
            startActivity(intent);
        });
    }

    public void getBannerError(String msg) {
        dismissLoading();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mRollPagerView.stopNestedScroll();
    }
}
